package org.dingfugui.triple.bean;

import org.dingfugui.triple.common.Logger;
import org.dingfugui.triple.common.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by fugui.ding on 2015/6/11.
 */
public class ProducerImpl implements Producer {

    private static final Logger logger = LoggerFactory.getLogger(ProducerImpl.class);

    private Class type;
    private String name;
    private Object instance;

    public ProducerImpl(Class type, String name, Object instance) {
        this.type = type;
        this.name = name;
        this.instance = instance;
    }

    @Override
    public Object invoke(String method, Class[] types, Object[] params) {
        try {
            Method m = type.getDeclaredMethod(method, types);
            Object result = m.invoke(instance, params);
            return result;
        } catch (NoSuchMethodException e) {
            logger.error("方法不存在{}({})", method, types);
        } catch (InvocationTargetException e) {
            logger.error("执行方法异常" + method, e);
        } catch (IllegalAccessException e) {
            logger.error("执行方法安全异常" + method, e);
        }
        return null;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Object getInstance() {
        return instance;
    }

    @Override
    public Class getType() {
        return type;
    }
}
